package org.ala.tiktools_clickhouse_generator.generator;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/**
 *
 *	@author ala
 *	@date 2025年6月9日
 */
public abstract class AbstractCreator {
	
	
    protected String getOutRootPath() {
//        return this.getClass().getClassLoader().getResource("").getPath() + "out/";
    	return "out/";
    }
    protected Configuration conf;

    protected Configuration getConfiguration() throws IOException {
        if (conf != null) {return conf;}

        conf = new Configuration(Configuration.VERSION_2_3_31);
        String path = this.getClass().getClassLoader().getResource("mybatis_template").getPath();
        conf.setDirectoryForTemplateLoading(new File(path));
        return conf;
    }

    protected Template getTemplate(String name) throws IOException {
        Configuration conf = getConfiguration();
        Template tpl = conf.getTemplate(name);
        tpl.setOutputEncoding("utf-8");
        return tpl;
    }
    protected void output(String tpl, Map<String, Object> dateMeta, String out) throws IOException, TemplateException {
        Template template = getTemplate(tpl);

        FileWriter fw = new FileWriter(out);
        template.process(dateMeta, fw);
        fw.flush();
        fw.close();
    }
}
